package com.example.myrecipes;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;

import android.os.Parcel;
import android.os.Parcelable;

public class Recipe implements Parcelable {

	private String recipeName;
	private HashSet<String> ingredients;
	private ArrayList<String> instructions;
	private String yield = "";
	private String prepTime = "";
	private String cookTime = "";
	private String readyInTime = "";

	public Recipe() {
		recipeName = "";
		ingredients = new HashSet<String>();
		instructions = new ArrayList<String>();
	}

	public Recipe(String recipeName) {
		this.recipeName = recipeName;
		ingredients = new HashSet<String>();
		instructions = new ArrayList<String>();
	}
	
	public Recipe(Parcel in) {
		recipeName = in.readString();
		yield = in.readString();
		prepTime = in.readString();
		cookTime = in.readString();
		readyInTime = in.readString();
		
		ingredients = (HashSet<String>) in.readSerializable();
		/*String[] arr = (String[]) in.readArray(Recipe.class.getClassLoader());
		for(int i = 0; i < arr.length; i++) {
			System.out.println( "TAG " + arr[i]);
		}*/
		//ingredients = (HashSet<String>) Arrays.asList(arr);
		instructions = (ArrayList<String>) in.readSerializable();

		
	}
	
	public void setRecipeName(String recipeName) {
		this.recipeName = recipeName;
	}

	public String getRecipeName() {
		return recipeName;
	}

	public void setYield(String yield) {
		this.yield = yield;
	}
	
	public String getYield() {
		return yield;
	}
	
	public void setPrepTime(String prepTime) {
		this.prepTime = prepTime;
	}
	
	public String getPrepTime() {
		return prepTime;
	}
	
	public void setReadyInTime(String readyInTime) {
		this.readyInTime = readyInTime;
	}
	
	public String getReadyInTime() {
		return readyInTime;
	}
	
	public void setCookTime(String cookTime) {
		this.cookTime = cookTime;
	}
	
	public String getCookTime() {
		return cookTime;
	}
	
 	public void addIngredient(String ingredientName) {
		ingredients.add(ingredientName);
	}

	public HashSet<String> getIngredients() {
		return ingredients;
	}

	public void addInstructions(String instruction) {
		instructions.add(instruction);
	}

	public ArrayList<String> getInstructions() {
		return instructions;
	}
	
	public void deleteIngredient(String ingredient) {
		ingredients.remove(ingredient);
	}
	
	public void deleteInstruction(String instruction) {
		Iterator<String> it = instructions.iterator();
		while (it.hasNext()) {
			String curInstruction = it.next();
			if (curInstruction.equals(instruction)) {
				it.remove();
			}
		}
	}
	
	public boolean equals(Recipe other) {
		return this.recipeName == other.recipeName;
	}

	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}

	public static final Parcelable.Creator<Recipe> CREATOR = new Parcelable.Creator<Recipe>() {		
		@Override
		public Recipe createFromParcel(Parcel source) {
			return new Recipe(source);
		}
		public Recipe[] newArray(int size) {
			return new Recipe[size];
		}
	};
	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeString(recipeName);	
		dest.writeString(yield);
		dest.writeString(prepTime);
		dest.writeString(cookTime);
		dest.writeString(readyInTime);
		dest.writeSerializable(ingredients);
		dest.writeSerializable(instructions);
		
	}
	
	public String toString() {
		return recipeName;
	}
}
